﻿var PageNumber = 10; // page count of dataset, default is 10.
var m_model = 0;//default model is for search(0), 1 for add model.

var d=new Date();
var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var TODAY = monthname[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();

function changeModel(model)
{
    if(model == 0)
    {
        m_model = 1;
        document.getElementById("ModelAdd").style.visibility = "hidden";
        document.getElementById("ModelSearch").style.visibility = "visible";
        //$("#pager").css("visibility","hidden");
        $("#pager").empty();
        $("#pager").append("<input id = 'addResult' type = 'button' value = '提交' onclick = 'addProduct()' style = 'visibility:hidden;' />");
        $("#pager").append("<input id = 'back2Add' type = 'button' value = '返回' onclick = 'back2Add()' style = 'visibility:hidden;' />");
        $("#main").empty();
//        $("#Links").css("visibility","hidden");
//        $("#btnExcelWriter").css("visibility","hidden");
        
    }
    else
    {
        m_model = 0;
        document.getElementById("ModelAdd").style.visibility = "visible";
        document.getElementById("ModelSearch").style.visibility = "hidden";
        $("#pager").empty();
        $("#main").empty();
//        $("#Links").css("visibility","visible");
//        $("#btnExcelWriter").css("visibility","visible");

    }
    
}

//
function addProduct()
{
    var datatype = $("#curButton").val();
    
    var strQueryCondition="";
    var strQueryConditionArray = $("#main").children();
    var tableProduct = strQueryConditionArray[0];
    
//    var tableProduct = $("#editTable");
    var nRows = tableProduct.rows.length;
    //alert(nRows);
    var bValue = 0; //用于记录是否填写了记录值，0表示未填写，1表示已填写；
    for(var i=1;i<nRows;i++)
    {
        //if(tableProduct.rows[i].cells[1].firstChild.value.length > 0) bValue = 1;
        //strQueryCondition += tableProduct.rows[i].cells[0].textContent + "=" + tableProduct.rows[i].cells[1].firstChild.value + ",";
        //strQueryCondition += tableProduct.rows[i].cells[0].textContent + ",";
        strQueryCondition += tableProduct.rows[i].cells[0].innerHTML + ",";
    }
    strQueryCondition = strQueryCondition.substring(0,strQueryCondition.length-1) + "=";
    for(var i=1;i<nRows;i++)
    {
        if(tableProduct.rows[i].cells[1].firstChild.value.length > 0) bValue = 1;
        //strQueryCondition += tableProduct.rows[i].cells[0].textContent + "=" + tableProduct.rows[i].cells[1].firstChild.value + ",";
        strQueryCondition += tableProduct.rows[i].cells[1].firstChild.value + ",";
    }
    //strQueryCondition = strQueryCondition.substring(0,strQueryCondition.length-1);
    if(bValue == 0)
    {
        alert("您未填写任何数据！");
        return;
    }
    var url = "WebServices/addDataSet.aspx";
    $.getJSON(url,{DATATYPE:datatype,QUERYSTRING:strQueryCondition,TEMP:Math.random()},function(json){
        //var n = json.length;
        if(json.result == "OK")
            alert("数据更新成功！");
        else
            alert("数据更新失败，请重新检查！");

    });    
    return strQueryCondition;



}

//clear search condition div content.
function ClearCondition()
{
    $("#divConditionExp").empty();
}

//Add search condition content to div
function AddCondition()
{
    var cond_total_exp = ""; //Expression of search condition
    var cond_result = "";  //Result of search condition
    var cond_param_id = ""; //Element ID of selected condition
    var cond_param_value = ""; //Element Value of selected condition
    
    //get user input.
    cond_param_id = $("#sltCondition").val();
    cond_param_value = $("#sltCondition").find("option:selected").text();
    cond_result = $("#txtCondition").val();
    cond_total_exp = cond_param_value + "=" + cond_result;
    
    //clear textbox
    $("#txtCondition").val("");
    
    //verify the condition expression
    if(cond_result.length == 0)
    {
        alert("请在文本框中填入条件值，谢谢！");
        return;
    }

    
    //verify the search condition of user input is duplicate or not.
    var Children = $("#divConditionExp").find("div");
    var htmltext = Children.text();
    if(htmltext != null)
    {
        if(htmltext.indexOf(cond_param_value) != -1) 
        {
            alert("请勿重复输入查询条件，谢谢！");
            return;
        }
    }
    
    //generate expression of search condition and display it on div.
    //generate a checkbox for user to enable the search condition or not.
    var chkResult =  document.createElement("input");
    chkResult.type = "checkbox";
    chkResult.id = "cond_" + cond_param_id;
    chkResult.checked = true;

    var txtResultExp  = document.createTextNode(cond_total_exp);
    
    var divCondition = document.createElement("div");
    divCondition.id = "div_" + cond_param_id; 
    
    $(divCondition).append(chkResult);
    $(divCondition).append(txtResultExp);
    $(divCondition).append("<br/>");
    
    $("#divConditionExp").append(divCondition);
}

//get result when user click the SEARCH button.
function GetDataBySearch()
{
    var datatype = $("#curButton").val();
    var language = $("#sltLanguage").val();
    
    var strQueryCondition = "";
    
    var strQueryConditionArray = $("#divConditionExp").children();
    for(var i=0;i<strQueryConditionArray.length;i++)
    {
        strQueryCondition += strQueryConditionArray[i].textContent + ",";
    }
    
    
    
    var url = "WebServices/getDataSet.aspx";
    $.get(url,{DATATYPE:datatype,LANGUAGE:language,PAGENUMBER:1,QUERYSTRING:strQueryCondition,TEMP:Math.random()},function(data){
        var xmlDocument = data.documentElement;
        var pageNumberNode = xmlDocument.getElementsByTagName("Pager");
        if (pageNumberNode.length == 1) {
            PageNumber = parseInt(pageNumberNode[0].attributes[0].nodeValue);
            $("#pager").pager({ pagenumber: 1, pagecount: PageNumber, buttonClickCallback: PageClick });
        }        
        var nodes = xmlDocument.getElementsByTagName("Result");
        
        var nNodes = nodes.length;
        
        if(nNodes > 0)
        {
            var TableNamesArray = new Array();
            var TableValuesArray = new Array();
            var nColumns = nodes[0].attributes.length;
            
            for(var i = 0; i < nColumns; i++)
            {
                TableNamesArray[i] = document.createTextNode(nodes[0].attributes[i].nodeName);
                TableValuesArray[i] = new Array();
                for(var j = 0; j < nodes.length; j++)
                {
                    TableValuesArray[i][j] = document.createTextNode(nodes[j].attributes[i].nodeValue);
                }
            }
            
            
            var dataTable = new DataTable(TableNamesArray,TableValuesArray,2);
            $(dataTable).css("searchtable");
            with($("#main"))
            {
                empty();
                append(dataTable);
                css("visibility", "visible");
            }
            $("#btnExcelWriter").css("visibility","visible");
            
        }
        else
        {
            $("#main").empty();
            $("#btnExcelWriter").css("visibility","hidden");
        }
        
    });
    
}

//get result when user click the PAGER button.
function GetDataByPager(pagenumber)
{
    var datatype = $("#curButton").val();
    var language = $("#sltLanguage").val();
        
    var strQueryCondition = "";
    
    var strQueryConditionArray = $("#divConditionExp").children();
    for(var i=0;i<strQueryConditionArray.length;i++)
    {
        strQueryCondition += strQueryConditionArray[i].textContent + ",";
    }
    
    
    
    var url = "WebServices/getDataSet.aspx";
    $.get(url,{DATATYPE:datatype,LANGUAGE:language,PAGENUMBER:pagenumber,QUERYSTRING:strQueryCondition,TEMP:Math.random()},function(data){
        var xmlDocument = data.documentElement;
        var nodes = xmlDocument.getElementsByTagName("Result");
        
        var nNodes = nodes.length;
        if(nNodes > 0)
        {
            var TableNamesArray = new Array();
            var TableValuesArray = new Array();
            var nColumns = nodes[0].attributes.length;
            for(var i = 0; i < nColumns; i++)
            {
                TableNamesArray[i] = document.createTextNode(nodes[0].attributes[i].nodeName);
                TableValuesArray[i] = new Array();
                for(var j = 0; j < nodes.length; j++)
                {
                    TableValuesArray[i][j] = document.createTextNode(nodes[j].attributes[i].nodeValue);
                }
            }
            var dataTable = new DataTable(TableNamesArray,TableValuesArray,2);
            $(dataTable).css("searchtable");
            with($("#main"))
            {
                empty();
                append(dataTable);
                css("visibility", "visible");
            }
            $("#btnExcelWriter").css("visibility","visible");
        }
        else
        {
            $("#main").empty();
            $("#btnExcelWriter").css("visibility","hidden");
        }
    });
}

//get result when user click the PRODUCT button.
function GetDataAll(datatype)
{
    
    $("#curButton").val(datatype);
    
    var url = "WebServices/getDataSet.aspx";
    
    if(m_model == 0){
        //$.get(url, { DATATYPE: datatype, LANGUAGE: language, PAGENUMBER: 1, TEMP: Math.random() }, function (data) {
        $.get(url, { DATATYPE: datatype, PAGENUMBER: 1, TEMP: Math.random() }, function(data) {
            var xmlDocument = data.documentElement;
            var pageNumberNode = xmlDocument.getElementsByTagName("Pager");
            if (pageNumberNode.length == 1) {
                PageNumber = parseInt(pageNumberNode[0].attributes[0].nodeValue);
                $("#pager").pager({ pagenumber: 1, pagecount: PageNumber, buttonClickCallback: PageClick });
            }

            var nodes = xmlDocument.getElementsByTagName("Result");

            var nNodes = nodes.length;
            if (nNodes > 0) {
                var TableNamesArray = new Array();
                var TableValuesArray = new Array();
                var nColumns = nodes[0].attributes.length;
                var btnDetails = new Array();
                var btnDelete = new Array();

                for (var i = 0; i < nColumns + 1; i++) {
                    if (i == nColumns) {
                        TableNamesArray[i] = document.createTextNode("编辑");
                        TableValuesArray[i] = new Array();
                        TableNamesArray[i+1] = document.createTextNode("删除");
                        TableValuesArray[i+1] = new Array();
                                                
                        for (var j = 0; j < nodes.length; j++) {
                            btnDetails[j] = document.createElement("input");
                            btnDetails[j].type = "button";
                            btnDetails[j].id = "btnDetails" + i;
                            btnDetails[j].value = "编辑";
                            $(btnDetails[j]).bind("click", { RowsIndex: j, PageIndex: 1, ButtonType: "detail" }, handler);

                            btnDelete[j] = document.createElement("input");
                            btnDelete[j].type = "button";
                            btnDelete[j].id = "btnDelete" + i;
                            btnDelete[j].value = "删除";
                            $(btnDelete[j]).bind("click", { RowsIndex: j, PageIndex: 1 ,ButtonType:"delete"}, handler);
                            
                            //$(btnDetails[j]).bind("click",{id,"1"},function(data){});
                            TableValuesArray[i][j] = btnDetails[j];
                            TableValuesArray[i + 1][j] = btnDelete[j];
                        }
                    }
                    else {
                        TableNamesArray[i] = document.createTextNode(nodes[0].attributes[i].nodeName);
                        TableValuesArray[i] = new Array();
                        for (var j = 0; j < nodes.length; j++) {
                            TableValuesArray[i][j] = document.createTextNode(nodes[j].attributes[i].nodeValue);
                        }
                    }
                }

                var dataTable = new DataTable(TableNamesArray, TableValuesArray, 2);
                $(dataTable).css("searchtable");
                with ($("#main")) {
                    empty();
                    append(dataTable);
                    css("visibility", "visible");
                }
                $("#btnExcelWriter").css("visibility", "visible");

            }
            else {
                $("#main").empty();
                $("#btnExcelWriter").css("visibility", "hidden");
            }
        });
    }
    else
    {
        url = "WebServices/getColumnName.aspx";
        $.get(url, { DATATYPE: datatype, TEMP: Math.random() }, function (data) {
            var xmlDocument = data.documentElement;
            var nodes = xmlDocument.getElementsByTagName("Result");
            if (nodes.length > 0) {
                var TableNamesArray = new Array();
                var ColumnValuesArray = new Array();
                //var nColumns = nodes[0].attributes.length;

                for (var i = 0; i < nodes.length; i++) {
                    TableNamesArray[i] = document.createTextNode(nodes[i].attributes[0].nodeValue);
                    ColumnValuesArray[i] = document.createElement("input");
                    ColumnValuesArray[i].type = "text";
                    ColumnValuesArray[i].id = nodes[i].attributes[0].nodeName;
                    if(i==0)
                        ColumnValuesArray[i].disabled = "disabled";                
                }

                var TableValuesArray = new Array(ColumnValuesArray);
                var dataTable = new DataTable(TableNamesArray, TableValuesArray, 1);
                $(dataTable).css("searchtable");
                $(dataTable).attr("id","editTable");
                with ($("#main")) {
                    empty();
                    append(dataTable);
                    css("visibility", "visible");
                }
                $("#addResult").css("visibility", "visible");    
                $("#back2Add").css("visibility", "visible");                
                
            }
        });
    }
}

//function Pager
PageClick = function (pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: PageNumber, buttonClickCallback: PageClick });
    //$("#result").html("Clicked Page " + pageclickednumber);
    GetDataByPager(pageclickednumber);
}


//write search result to excel/
function Wirte2Excel()
{
    var datatype = $("#curButton").val();
    var language = $("#sltLanguage").val();
    
    var strQueryCondition = "";
    
    var strQueryConditionArray = $("#divConditionExp").children();
    for(var i=0;i<strQueryConditionArray.length;i++)
    {
        strQueryCondition += strQueryConditionArray[i].textContent + ",";
    }
    
    
    
    var url = "WebServices/getDataSet.aspx";
    $.get(url,{DATATYPE:datatype,LANGUAGE:language,PAGENUMBER:0,QUERYSTRING:strQueryCondition,TEMP:Math.random()},function(data){
        var xmlDocument = data.documentElement;
        var pageNumberNode = xmlDocument.getElementsByTagName("Pager");
        if (pageNumberNode.length == 1) {
            var bSaveStatus = pageNumberNode[0].attributes[0].nodeValue;
            if(bSaveStatus == "True")
            {
                alert("导出到Excel文件成功！");
                var fileName = pageNumberNode[0].attributes[1].nodeValue;
                window.open("./data/" + fileName,"保存");
            }
            else
            {
                alert("导出到Excel文件失败，请检查查询条件是否正确！");
            }
        }
        else
        {
            alert("导出到Excel文件失败，请检查查询条件是否正确！");
        }        
        
    });

}

function Conjunction() {

    var url = "WebServices/getDataSet.aspx";

    //$.get(url, { DATATYPE: datatype, LANGUAGE: language, PAGENUMBER: 1, TEMP: Math.random() }, function (data) {
    $.get(url, { DATATYPE: datatype, PAGENUMBER: 1, TEMP: Math.random() }, function(data) {
        var xmlDocument = data.documentElement;
        var pageNumberNode = xmlDocument.getElementsByTagName("Pager");
        if (pageNumberNode.length == 1) {
            PageNumber = parseInt(pageNumberNode[0].attributes[0].nodeValue);
            $("#pager").pager({ pagenumber: 1, pagecount: PageNumber, buttonClickCallback: PageClick });
        }

        var nodes = xmlDocument.getElementsByTagName("Result");

        var nNodes = nodes.length;
        if (nNodes > 0) {
            var TableNamesArray = new Array();
            var TableValuesArray = new Array();
            var nColumns = nodes[0].attributes.length;
            var btnDetails = new Array();
            var btnDelete = new Array();

            for (var i = 0; i < nColumns + 1; i++) {
                if (i == nColumns) {
                    TableNamesArray[i] = document.createTextNode("编辑");
                    TableValuesArray[i] = new Array();
                    TableNamesArray[i + 1] = document.createTextNode("删除");
                    TableValuesArray[i + 1] = new Array();

                    for (var j = 0; j < nodes.length; j++) {
                        btnDetails[j] = document.createElement("input");
                        btnDetails[j].type = "button";
                        btnDetails[j].id = "btnDetails" + i;
                        btnDetails[j].value = "编辑";
                        $(btnDetails[j]).bind("click", { RowsIndex: j, PageIndex: 1, ButtonType: "detail" }, handler);

                        btnDelete[j] = document.createElement("input");
                        btnDelete[j].type = "button";
                        btnDelete[j].id = "btnDelete" + i;
                        btnDelete[j].value = "删除";
                        $(btnDelete[j]).bind("click", { RowsIndex: j, PageIndex: 1, ButtonType: "delete" }, handler);

                        //$(btnDetails[j]).bind("click",{id,"1"},function(data){});
                        TableValuesArray[i][j] = btnDetails[j];
                        TableValuesArray[i + 1][j] = btnDelete[j];
                    }
                }
                else {
                    TableNamesArray[i] = document.createTextNode(nodes[0].attributes[i].nodeName);
                    TableValuesArray[i] = new Array();
                    for (var j = 0; j < nodes.length; j++) {
                        TableValuesArray[i][j] = document.createTextNode(nodes[j].attributes[i].nodeValue);
                    }
                }
            }

            var dataTable = new DataTable(TableNamesArray, TableValuesArray, 2);
            $(dataTable).css("searchtable");
            with ($("#main")) {
                empty();
                append(dataTable);
                css("visibility", "visible");
            }
            $("#btnExcelWriter").css("visibility", "visible");

        }
        else {
            $("#main").empty();
            $("#btnExcelWriter").css("visibility", "hidden");
        }
    });


}

function handler(event)
{
    var rowIndex = event.data.RowsIndex;
    var pageIndex = event.data.PageIndex;
    var buttonType = event.data.ButtonType;
    
    var datatype = $("#curButton").val();

    if (buttonType == "detail") {
        var url = "WebServices/getData.aspx";

        $.get(url, { DATATYPE: datatype, ROWINDEX: rowIndex, PAGENUMBER: pageIndex, TEMP: Math.random() }, function(data) {
            var xmlDocument = data.documentElement;
            var nodes = xmlDocument.getElementsByTagName("Result");

            var nRes = nodes.length;
            if (nRes > 0) {
                var TableNamesArray = new Array();
                //var TableValuesArray = new Array();
                var nColumns = nodes[0].attributes.length;
                //var btnDetails = new Array();

                var ColumnValuesArray = new Array();

                for (var i = 0; i < nColumns; i++) {
                    TableNamesArray[i] = document.createTextNode(nodes[0].attributes[i].nodeName);
                    ColumnValuesArray[i] = document.createElement("input");
                    ColumnValuesArray[i].type = "text";
                    ColumnValuesArray[i].id = nodes[0].attributes[i].nodeName;
                    ColumnValuesArray[i].value = nodes[0].attributes[i].nodeValue;
                    if (i == 0)
                        ColumnValuesArray[i].disabled = "disabled";
                }
                var TableValuesArray = new Array(ColumnValuesArray);
                var dataTable = new DataTable(TableNamesArray, TableValuesArray, 1);

                $(dataTable).css("searchtable");
                with ($("#main")) {
                    empty();
                    append(dataTable);
                    css("visibility", "visible");
                }
                $("#pager").empty();
                $("#pager").append("<input id = 'refreshResult' type = 'button' value = '提交' onclick = 'refreshRecord()'/>");
                $("#pager").append("<input id = 'backtoList' type = 'button' value = '返回' onclick = 'backtoList()'/>");
                $("#Links").css("visibility", "hidden");
                $("#btnExcelWriter").css("visibility", "hidden");

            }
            else {
                $("#main").empty();
                $("#btnExcelWriter").css("visibility", "hidden");
            }
        });
    }
    else {
        if ((window.confirm("确认要删除这条记录吗？") == true)) {
            var url = "WebServices/deleteRecord.aspx";

            $.getJSON(url, { DATATYPE: datatype, ROWINDEX: rowIndex, PAGENUMBER: pageIndex, TEMP: Math.random() }, function(json) {
                if (json.result == "OK") {
                    alert("删除成功！");
                    GetDataAll(datatype);
                }
                else
                    alert("删除失败，请重试删除！");
            });
        }
    }
}

function refreshRecord()
{
    var datatype = $("#curButton").val();
    
    var strQueryCondition="";
    var strQueryConditionArray = $("#main").children();
    var tableProduct = strQueryConditionArray[0];
    var nRows = tableProduct.rows.length;
    var bValue = 0; //用于记录是否填写了记录值，0表示未填写，1表示已填写；
    for(var i=0;i<nRows;i++)
    {
        //if(tableProduct.rows[i].cells[1].firstChild.value.length > 0) bValue = 1;
        //strQueryCondition += tableProduct.rows[i].cells[0].textContent + "=" + tableProduct.rows[i].cells[1].firstChild.value + ",";
        strQueryCondition += tableProduct.rows[i].cells[0].innerHTML + ";";
    }
    strQueryCondition = strQueryCondition.substring(0,strQueryCondition.length-1) + "=";
    for(var i=0;i<nRows;i++)
    {
        //if(tableProduct.rows[i].cells[1].firstChild.value.length > 0) bValue = 1;
        //strQueryCondition += tableProduct.rows[i].cells[0].textContent + "=" + tableProduct.rows[i].cells[1].firstChild.value + ",";
        strQueryCondition += tableProduct.rows[i].cells[1].firstChild.value + ";";
    }
    //strQueryCondition = strQueryCondition.substring(0,strQueryCondition.length-1);
//    if(bValue == 0)
//    {
//        alert("您为填写任何数据！");
//        return;
//    }
    var url = "WebServices/updateDataSet.aspx";
    $.getJSON(url,{DATATYPE:datatype,QUERYSTRING:strQueryCondition,TEMP:Math.random()},function(json){
        //var n = json.length;
        if(json.result == "OK")
            alert("数据更新成功！");
        else
            alert("数据更新失败，请重新检查！");

    });    
    //return strQueryCondition;
}


function backtoList()
{
    var datatype = $("#curButton").val();
    GetDataAll(datatype);
}


function back2Add()
{
    //$("#pager").empty();
    
    $("#addResult").css("visibility", "hidden");    
    $("#back2Add").css("visibility", "hidden");                
    
    $("#main").empty();
}





// JScript 文件

//-------------------------------------------------------
//   Yang Xiaodong
//   IRSA, CAS
//-------------------------------------------------------
//Class     :DataTable
//Purpose   :provides a table class
//-------------------------------------------------------
//Calls     :
//Called by :
//-------------------------------------------------------
//Arguments :ID             the id attribute of the table
//           cssStyle       the css class of the table   
//           tableTitles    the Column or Row titles of table
//           tableValues    A 2 dimension Array to represent values in table
//           alignDirect    the title align direct of table, 0 is horizontal or titles in coloumns, 1 is vertical or titles in rows
//Globals   :
//Returns   :
//-------------------------------------------------------
//Notes     :
//-------------------------------------------------------
//History   :
//=======================================================


var DataTable = function(tableTitles, tableValues,alignDirect)
{
    var table = document.createElement("table");
    var tbody = document.createElement("tbody");
    table.appendChild(tbody);
    
    if (alignDirect == 0)         //Columns Mode
    {
        var nColumns = tableTitles.length;
        var nRows = tableValues.length + 1;
        for (var i = 0; i < nRows; i++)
        {
            tbody.insertRow(i);
            for (var j = 0; j < nColumns; j++)
            {
                tbody.rows[i].insertCell(j);
                if (i == 0)
                {
                    tbody.rows[i].cells[j].appendChild(tableTitles[j]);
                }
                else
                {
                    tbody.rows[i].cells[j].appendChild(tableValues[i-1][j]);
                }
            }
        }
    }
    else if(alignDirect == 1)                       //Rows Mode
    {
        var nRows = tableTitles.length;
        var nColumns = tableValues.length + 1;
        for(var i = 0; i < nRows; i++)
        {
            tbody.insertRow(i);
            for (var j = 0; j < nColumns; j++)
            {
                tbody.rows[i].insertCell(j);
                //tbody.rows[i].cells[j].setAttribute("align", "left");
                if (j == 0)
                {
                    tbody.rows[i].cells[j].appendChild(tableTitles[i]);
                }
                else
                {
                    tbody.rows[i].cells[j].appendChild(tableValues[j-1][i]);
                }
            }
        }
    }
    else if (alignDirect == 2)                      //标题在第一行，后续每行添加一组数据，数据以标题类型组织，类似于数据库中的表格方式
    {
        var nRows = tableValues[0].length + 1;// tableTitles.length;
        var nColumns = tableTitles.length;//tableValues.length + 1;
        for (i = 0; i < nRows; i++)
        {
            tbody.insertRow(i);
            for (j = 0; j < nColumns; j++)
            {
                tbody.rows[i].insertCell(j);
            }
        }
        
        for(i = 0; i < nColumns; i++)
        {
            for (j = 0; j < nRows; j++)
            {
                if (j == 0)
                {
                    tbody.rows[j].cells[i].appendChild(tableTitles[i]);
                }
                else
                {
                    tbody.rows[j].cells[i].appendChild(tableValues[i][j-1]);
                }
            }
        }
        
    }
    return table;
}
